﻿jQuery.extend(jQuery.prototype, {
    pageV: function (settings) {
        var thisobj = this;
        var options = jQuery.extend({
            pagenum: 1, //当前页是第几页,默认第一页
            page: 10, //一页显示多少条，默认10条
            pages: 0, 	//一共多少页
            pageCount: 0, //一共有多少条数据
            splitNum: 5, //显示第一页到第几页，当前页的前后几页，以及最后一页的前面几页，其他页数都用…代替 默认间隔为5
            onClick: function (pagenum) { }
        }, settings);

        if (options.pages == 0 && options.pageCount == 0) return;
        if (options.pages == 0 && options.pageCount != 0) {
            options.pages = Math.ceil(options.pageCount / options.page);
        }

        if (options.pages < 2) return;

        var pagehtml = "";
        if (options.pagenum > 1) {
            pagehtml += '<a class="previous_page">上一页</a>';
        } else {
            pagehtml += "<span class='previous_page disabled'>上一页</span>";
        }
        var flag = false;
        for (var i = 1; i <= options.pages; ++i) {
            if (i == options.pagenum) {
                pagehtml += "<em>" + i + "</em>";
            }
            else if (i < options.splitNum + 1 || i > options.pages - options.splitNum || (i > options.pagenum - options.splitNum && i < options.pagenum + options.splitNum)) {
                pagehtml += '<a>' + i + '</a>';
                flag = false;
            }
            else if (((i == options.splitNum + 1 && i < options.pages - options.splitNum && i < options.pagenum - options.splitNum)
        || (i == options.pages - options.splitNum && i >= options.splitNum + 1 && i >= options.pagenum + options.splitNum)
        || (i == options.pagenum - options.splitNum && i >= options.splitNum + 1 && i <= options.pages - options.splitNum)
        || (i == options.pagenum + options.splitNum && i >= options.splitNum + 1 && i <= options.pages - options.splitNum)) && flag == false) {
                pagehtml += "<span>...</span>";
                flag = true;
            }
        }

        if (options.pagenum < options.pages) {
            pagehtml += '<a class="next_page">下一页</a>';
        }
        else {
            pagehtml += "<span class='next_page disabled'>下一页</span>";
        }

        thisobj.html(pagehtml);

        thisobj.find("a").click(function () {
            options.onClick($(this).html());
        });

        thisobj.find("a.previous_page").eq(0).click(function () {
            var nextnum = options.pagenum - 1;
            thisobj.pageV({
                pagenum: nextnum,
                page: options.page,
                pages: options.pages,
                splitNum: options.splitNum,
                onClick: options.onClick(nextnum)
            });
        });

        thisobj.find("a.next_page").eq(0).click(function () {
            var nextnum = options.pagenum + 1;
            thisobj.pageV({
                pagenum: nextnum,
                page: options.page,
                pages: options.pages,
                splitNum: options.splitNum,
                onClick: options.onClick(nextnum)
            });
        });
    }
});